package com.newly.center.sys.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.newly.api.sys.feign.OperLoggerClient;
import com.newly.center.sys.service.menu.api.IMenuService;
import com.newly.center.sys.service.tenant.api.ITenantService;
import com.newly.common.base.annotation.NewlyLog;
import com.newly.common.base.constants.MenuConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.PageUtils;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.sys.menu.vo.MenuAbilityVo;
import com.newly.common.base.entity.sys.operLogger.dto.OperLoggerDto;
import com.newly.common.base.entity.sys.operLogger.po.OperLoggerPo;
import com.newly.common.base.entity.sys.operLogger.vo.OperLoggerVo;
import com.newly.common.base.entity.sys.tenant.bo.SimpleTenantBo;
import com.newly.common.base.entity.sys.tenant.vo.SimpleTenantVo;
import com.newly.common.core.service.operLogger.api.IOperLoggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: newly-parent
 * @remark: 操作日志接口
 * @author: guan
 * @create: 2021-12-04 18:05
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_SYS_OPERATION_LOGGER)
public class OperLoggerApi implements OperLoggerClient {

    @Autowired
    private IOperLoggerService operLoggerService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private ITenantService tenantService;

    @Override
    public R create(OperLoggerDto operLoggerDto) {
        boolean result = false;
        try {
            result = operLoggerService.save(operLoggerDto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @Override
    public R change(OperLoggerDto operLoggerDto) {
        boolean result = false;
        try {
            result = operLoggerService.modify(operLoggerDto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @NewlyLog(ability = MenuConstant.Ability.QUE, title = "操作日志", isOpen = false)
    @Override
    public R list(Integer offset, Integer limit, Integer abilityCode,
                  String fromTime, String toTime, String keyword) {
        PageUtils pageUtils = null;
        try {
            pageUtils = operLoggerService.list(offset, limit, abilityCode,
                    fromTime, toTime, keyword);
            if (!ObjectUtil.isEmpty(pageUtils) && !ObjectUtil.isEmpty(pageUtils.getList())) {
                List<OperLoggerPo> operLoggerPos = pageUtils.getList();
                List<OperLoggerVo> operLoggerVos = operLoggerPos.stream().map(operLoggerPo -> {
                    OperLoggerVo operLoggerVo = BeanUtil.copyProperties(operLoggerPo, OperLoggerVo.class);
                    operLoggerVo.setAbility(MenuConstant.Ability.getInstance(operLoggerPo.getAbilityCode()));
                    SimpleTenantBo simpleTenantBo = tenantService.getSimpleTenant(operLoggerPo.getUserId());
                    if (!ObjectUtil.isEmpty(simpleTenantBo)) {
                        operLoggerVo.setTenant(BeanUtil.copyProperties(simpleTenantBo,
                                SimpleTenantVo.class));
                    }
                    return operLoggerVo;
                }).collect(Collectors.toList());
                pageUtils.setList(operLoggerVos);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(pageUtils);
    }

    @Override
    public R queryByIds(List<String> logIds) {
        List<OperLoggerVo> operLoggerVos = null;
        try {
            List<OperLoggerPo> operLoggerPos = operLoggerService.getByIds(logIds);
            if (ObjectUtil.isNotEmpty(operLoggerPos)) {
                operLoggerVos = BeanUtil.copyToList(operLoggerPos, OperLoggerVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(operLoggerVos);
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "操作日志", isOpen = true)
    @Override
    public R deleteByIds(List<String> logIds) {
        boolean result = false;
        try {
            result = operLoggerService.removeByIds(logIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "操作日志", isOpen = false)
    @Override
    public R clear() {
        boolean result = false;
        try {
            result = operLoggerService.clear();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @Override
    public R queryOperation() {
        List<MenuAbilityVo> abilityByTypeCode = null;
        try {
            abilityByTypeCode = menuService.getAbilityByTypeCode(MenuConstant.Type.BTN.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(abilityByTypeCode);
    }
}
